//
//  RSWRESTService+Attendance.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 17/1/2.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "RSWRESTService+Attendance.h"

@implementation RSWRESTService (Attendance)

-(XLGResult*)getMyAttendanceToken:(NSString*)token  pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize
{
    NSString *URI = [NSString stringWithFormat:@"api/mobilekaoqin?size=%ld&page=%ld&typeid=&token=%@",(long)pageSize, (long)pageIndex, token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [AttendanceList JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[AttendanceList class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}

-(XLGResult*)getMyAttendanceTypeToken:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/mobilekaoqin?token=%@",token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [SimpleActionSheetInfo JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[SimpleActionSheetInfo class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}

-(XLGResult*)addAttendance:(AttendanceInfo *)info token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/mobilekaoqin?isinside=%ld&latitude=%f&address=%@&content=%@&longitude=%f&type=%@&token=%@",
                     (long)[info.isinside intValue],
                     [info.latitude doubleValue],
                     info.address,
                     info.content,
                     [info.longitude doubleValue],
                     info.typeId,
                     token];
    
    NSDictionary *dic = @{
                          @"isinside":@([info.isinside intValue]),
                          @"latitude":@([info.latitude doubleValue]),
                          @"address":info.address,
                          @"content":info.content,
                          @"longitude":@([info.longitude doubleValue]),
                          @"type":info.typeId,
                          @"token":token
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            NSString *value = [r.outObj[@"Value"] stringValue];
            return [XLGResult resultWithOutResult:r.outResult outObj:value];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:nil outError:error];
        }
    }
    
    return r;
}
@end
